package scheduler;

import java.util.*;

public class SchedulerMethods extends SchedulerData{

    public enum Keywords {
        ;
        private static final String GROUP = "GROUP *  |";
        private static final String[] DAYS = new String[]{" MONDAY  ", " TUESDAY ", "WEDNESDAY", "THURSDAY ", " FRIDAY  "};
        private static final String[] PAIRS = new String[]{"|              1            | ", "|              2            | ", "|              3            | ", "|              4            | ", "|              5            | ", "|              6            | "};
    };

    public static Boolean VerifyOverlapsAndPrintScheduler(HashSet<List<List<String>>> globalList, List<List<String>> listOfEvents, List<String> event) {
        String findOverlapInClasses = null;
        String overlapInTeachers = null;
        List<List<String>> copyOfListOfEvents = new ArrayList<List<String>>(listOfEvents);

        for (List<String> events : listOfEvents) {
            if ((events.get(0) + events.get(1)).equals(event.get(0) + event.get(1))) {
                return null;
            }
        }

        copyOfListOfEvents.add(event);
        int time = copyOfListOfEvents.indexOf(event);

        for (List<List<String>> allGroups : globalList) {
            try {
                findOverlapInClasses = allGroups.get(time).get(3);
                overlapInTeachers = allGroups.get(time).get(2);
            } catch (IndexOutOfBoundsException e) {
            } catch (Exception e) {
                e.printStackTrace();
            }
            if ((findOverlapInClasses.equals(event.get(3))) || (overlapInTeachers.equals(event.get(2)))) {
                return null;
            }
        }
        listOfEvents.add(event);
        System.out.print(event + " ");
        return true;
    }

    public static HashSet<String> dataAboutGroupsSet() {
        return GROUPS;
    }

    public static List<String> getOneEvent() {

        List<String> UniqueEventForEachGroup = new ArrayList<String>();

        String getTheSubject = SchedulerMethods.getRandomFromList(LIST_OF_SUBJECTS);
        int numberOfSuitableTeachersGroup = LIST_OF_SUBJECTS.indexOf(getTheSubject);
        List<String> SuitableTeachers = LIST_OF_ALL_TEACHERS.get(numberOfSuitableTeachersGroup);

        UniqueEventForEachGroup.add(SchedulerMethods.getRandomFromList(LIST_OF_SUBJECTS));
        UniqueEventForEachGroup.add(SchedulerMethods.getRandomFromSet(IF_LECTURE_SET));
        UniqueEventForEachGroup.add(SchedulerMethods.getRandomFromList(SuitableTeachers));
        UniqueEventForEachGroup.add(SchedulerMethods.getRandomFromSet(SET_OF_AUDITORIUMS));

        return UniqueEventForEachGroup;
    }

    public static Boolean printHeader() {
        List<String> daysOfWeek = Arrays.asList(Keywords.DAYS);
        List<String> numberOfPair = Arrays.asList(Keywords.PAIRS);
        System.out.print(Keywords.GROUP);
        for (String day : daysOfWeek) {
            printSeveralTimes(day);
        }
        System.out.print("\n  *   *  |");
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 6; j++) {
                System.out.print(numberOfPair.get(j));
            }
        }
        return true;
    }

    public static String printSeveralTimes(String day) {
        for (int i = 0; i < 6; i++) {
            System.out.print("|          " + day + "        | ");
        }
        return "|          " + day + "        | ";
    }

    public static String getRandomFromList(List<String> strings) {
        Random randomGenerator = new Random();
        int index = randomGenerator.nextInt(strings.size());
        String item = strings.get(index);
        return item;
    }

    public static String getRandomFromSet(Set<String> strings) {
        int size = strings.size();
        int item = new Random().nextInt(size);
        int i = 0;
        for (String s : strings) {
            if (i == item)
                return s;
            i++;
        }
        return "";
    }

    public static String printNewLine(String group) {
        System.out.print("\n* " + group + " * | ");
        return ("\n* " + group + " * | ");
    }
}
